﻿using MapGeneration;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoleCustom.Main;
using System.Linq;
using UnityEngine;
using Utils;


namespace RoleCustom.Event
{
    public class SCP3114Event
    {
        [PluginEvent(ServerEventType.PlayerSpawn)]
        public void OnPlayerSpawn(PlayerSpawnEvent ev)
        {
            if (ev.Player.Role != PlayerRoles.RoleTypeId.Scp3114) return;

            TimeCoroutine.Delay(0.5f, () =>
            {
                PlayerUtils.SetHP(ev.Player,
                    CodeLoad.Singleton.Config.SCP3114Event.Health,
                    CodeLoad.Singleton.Config.SCP3114Event.Health);

                PlayerUtils.SetAP(ev.Player,
                    CodeLoad.Singleton.Config.SCP3114Event.AHealth,
                    CodeLoad.Singleton.Config.SCP3114Event.AHealth);

                if (Round.IsLocked) return;

                this.SetSpawnPoint(ev);
                this.SendBroadcast(ev);
            });
        }


        private void SetSpawnPoint(PlayerSpawnEvent ev)
        {
            RoomIdentifier match = Map.Rooms.ToList().First(room => room.Name == RoomName.Lcz173);
            if (match == null)
            {
                Log.Info("SCP173Event|查询房间失败");
                return;
            }

            GameObject point = new GameObject();
            point.transform.parent = match.ApiRoom.GameObject.transform;
            point.transform.localPosition = new Vector3(16.5f, 13f, 8f);
            ev.Player.Position = point.transform.position;
        }
        private void SendBroadcast(PlayerSpawnEvent ev)
        {
            string message = "嘿！<color=red>SCP-3114</color> 配合<color=red>己方阵营</color>清除设施内的所有敌人！| 收容门将在<color=red>15s</color>后失效，但仍需要你交互开启!";
            MessageStringUtils.GernerateMissionMessage(message, out string output);
            ev.Player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Truncated, true);
        }


        string[] commands = new string[] { "as3114" };


        [PluginEvent(ServerEventType.RemoteAdminCommandExecuted)]
        public void OnRemoteCommandRun(RemoteAdminCommandExecutedEvent ev)
        {
            bool isContain = commands.Contains(ev.Command);
            if (!isContain) return;

            bool isTry = int.TryParse(ev.Arguments[0], out int output);
            if (!isTry) return;


            var theMatch = Player.GetPlayers().Find(e => e.PlayerId == output);
            if (theMatch == null) return;

            theMatch.SetRole(PlayerRoles.RoleTypeId.Scp3114);
        }




        [PluginEvent(ServerEventType.RoundStart)]
        private void OnRoundStart(RoundStartEvent ev)
        { 
            if (Player.GetPlayers().Count < 20) return;

            if (Round.IsLocked) return;

            bool isLuck = UnityEngine.Random.Range(0, 100) <= 10;

            if (isLuck)
            {
                TimeCoroutine.Delay(10f, () =>
                {
                    bool isExist = Player.GetPlayers().Find(e => e.Role == PlayerRoles.RoleTypeId.Scp3114) != null;
                    if (!isExist)
                    {

                        Player player = Player.GetPlayers().First(e => e.Role == PlayerRoles.RoleTypeId.ClassD);

                        if (player == null)
                        {
                            return;
                        }

                        MessageStringUtils.GenerateAdminMessage("被选中为 SCP-3114 演绎者 | 预计 15f 后开始演绎", out string output);
                        player.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Normal, true);

                        TimeCoroutine.Delay(15f, () => player.SetRole(PlayerRoles.RoleTypeId.Scp3114));
                    }
                });
            }

        }
    }
}
